<template>
  <!--  基本资料-->
  <div style="text-align: center" v-loading="loading">
    <el-row :gutter="20">
      <el-col :xs="24" :sm="10">
        <div class="avatar">
                    <el-tooltip class="item" effect="dark" content="点击修改头像" placement="right">
                      <el-upload
                          class="avatar-uploader"
                          action="" accept="jpg"
                          :show-file-list="false"
                          :on-success="handleAvatarSuccess"
                          :before-upload="beforeAvatarUpload">
                        <el-avatar :size="300" :src="userInfoData.avatar" fit="cover">
                          <img
                              alt=""
                              src=""
                          >
                        </el-avatar>
                      </el-upload>
                    </el-tooltip>
<!--          <el-avatar :size="300" :src="userInfoData.avatar" fit="cover">-->
<!--            <img-->
<!--                alt=""-->
<!--                src=""-->
<!--            >-->
<!--          </el-avatar>-->
        </div>
      </el-col>
      <el-col :xs="24" :sm="14">
        <el-form label-width="100px" :model="userInfoData">
          <el-form-item label="用户名" prop="username">
            <el-input disabled v-model="userInfoData.username"></el-input>
          </el-form-item>
          <el-form-item label="所在城市" prop="city">
            <el-input v-model="userInfoData.city"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="userInfoData.email"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitUserInfo">提交</el-button>
            <el-button type="info" @click="resetUserInfo">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {updateUserinfo, uploadUserIcon} from "@/api/sys/user";

export default {
  name: "Profile",
  data() {
    return {
      imgUsl: '',
      // userInfoData: {},
      loading: false
    }
  },
  methods: {
    //提交表单
    submitUserInfo() {
      const data = {
        city: this.userInfoData.city,
        email: this.userInfoData.email
      }
      updateUserinfo(data).then(res => {
        this.$message({
          type: "success",
          message: "更新成功"
        })
        this.$store.dispatch("getUserInfo")
      })
    },
    //重置表单
    resetUserInfo() {
      this.$store.dispatch("getUserInfo")
    },

    //图片上传成功
    handleAvatarSuccess() {
    },
    //上传之前的钩子
    beforeAvatarUpload(file) {
      //校验成功之后上传至服务器
      if (this.validate(file)) {
        const formData = new FormData()
        formData.append('file', file)
        uploadUserIcon(formData).then(res => {
          this.$store.dispatch("getUserInfo")
          console.log(res)
        })
      }

    },
    //头像校验
    validate(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    }
  },
  computed: {
    userInfoData: {
      get() {
        return this.$store.state.user.userInfo
      }
    }
  },
}
</script>

<style scoped>
.el-form {
  width: 420px;
  margin: 50px auto;
}


.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
</style>
